<template>
  <li>
    <label>
      <input type="checkbox" :checked="todo.done" @change="checkTodo(todo.id)"/>
      <span v-show="!todo.isEdit">{{ todo.action }}</span>
      <input type="text"
             v-show="todo.isEdit"
             :value="todo.action"
             @keyup.enter="editDown(todo,$event)"
             @blur="editDown(todo,$event)">
    </label>
    <button class="btn btn-danger" @click="removeTodo(todo.id)">删除</button>
    <button class="btn btn-edit" @click="editTodo(todo)">编辑</button>
  </li>
</template>

<script>
import pubsub from 'pubsub-js'

export default {
  name: "MyItem",
  props: ['todo'],
  methods: {
    removeTodo(id) {
      pubsub.publish('removeTodo', id)
    },
    checkTodo(id) {
      this.$bus.$emit('checkTodo', id)
    },
    editTodo(todo) {
      if ('isEdit' in todo) {
        todo.isEdit = true
      } else {
        this.$set(todo, 'isEdit', true)
      }
    },
    editDown(todo,event) {
      this.$bus.$emit('updateTodo',todo.id,event.target.value)
      todo.isEdit = false
    }
  }
}
</script>

<style scoped>
/*item*/
li {
  list-style: none;
  height: 36px;
  line-height: 36px;
  padding: 0 5px;
  border-bottom: 1px solid #ddd;
}

li label {
  float: left;
  cursor: pointer;
}

li label li input {
  vertical-align: middle;
  margin-right: 6px;
  position: relative;
  top: -1px;
}

li button {
  float: right;
  display: none;
  margin-top: 3px;
}

li:before {
  content: initial;
}

li:last-child {
  border-bottom: none;
}

li:hover {
  background-color: #c1c1c1;
}

li:hover button {
  display: block;
}
</style>